package io.renren.modules.bus.controller;

import java.util.*;
import java.math.BigDecimal;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.renren.modules.bus.entity.MemberEntity;
import io.renren.modules.bus.service.MemberService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 会员管理
 * @Author lililiang
 * @Date 2019-02-23 15:07:23
 */
@RestController
@Api(tags = "会员管理")
@RequestMapping("/bus/member")
public class MemberController extends AbstractController{
    @Autowired
    private MemberService memberService;

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询", response = MemberEntity.class)
    @GetMapping("/list")
    @RequiresPermissions("bus:member:list")
    public R list(
            @ApiParam(value = "id", required = false) @RequestParam(required = false) Integer id,
                @ApiParam(value = "卡号", required = false) @RequestParam(required = false) String cardNo,
                @ApiParam(value = "手机号", required = false) @RequestParam(required = false) String mobile,
                @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String memberName,
                @ApiParam(value = "性别(1 男；2 女）", required = false) @RequestParam(required = false) Integer sex,
                @ApiParam(value = "当前积分", required = false) @RequestParam(required = false) Integer currentPoint,
                @ApiParam(value = "累计消费金额", required = false) @RequestParam(required = false) BigDecimal monetary,
                @ApiParam(value = "店铺id", required = false) @RequestParam(required = false) Integer storeId,
                @ApiParam(value = "家庭住址", required = false) @RequestParam(required = false) String familyAddress,
                @ApiParam(value = "Email", required = false) @RequestParam(required = false) String email,
                @ApiParam(value = "QQ", required = false) @RequestParam(required = false) String qq,
                @ApiParam(value = "会员等级", required = false) @RequestParam(required = false) Integer level,
                @ApiParam(value = "折扣比例", required = false) @RequestParam(required = false) Integer discount,
                @ApiParam(value = "创建人", required = false) @RequestParam(required = false) Integer author,
                @ApiParam(value = "创建时间", required = false) @RequestParam(required = false) Date createTime,
                @ApiParam(value = "修改人", required = false) @RequestParam(required = false) Integer modifier,
                @ApiParam(value = "修改时间", required = false) @RequestParam(required = false) Date modifyTime,
                @ApiParam(value = "状态，1有效，0无效，-1删除", required = false) @RequestParam(required = false) Integer status
        ){
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("cardNo", cardNo);
        params.put("mobile", mobile);
        params.put("memberName", memberName);
        params.put("sex", sex);
        params.put("currentPoint", currentPoint);
        params.put("monetary", monetary);
        params.put("storeId", storeId);
        params.put("familyAddress", familyAddress);
        params.put("email", email);
        params.put("qq", qq);
        params.put("level", level);
        params.put("discount", discount);
        params.put("author", author);
        params.put("createTime", createTime);
        params.put("modifier", modifier);
        params.put("modifyTime", modifyTime);
        params.put("status", status);
        PageUtils page = memberService.list4Page(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表查询
     */
    @ApiOperation(value = "列表查询", response = MemberEntity.class)
    @GetMapping("/listByCondition")
    @RequiresPermissions("bus:member:list")
    public R listByCondition(
            @ApiParam(value = "id", required = false) @RequestParam(required = false) Integer id,
                @ApiParam(value = "卡号", required = false) @RequestParam(required = false) String cardNo,
                @ApiParam(value = "手机号", required = false) @RequestParam(required = false) String mobile,
                @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String memberName,
                @ApiParam(value = "性别(1 男；2 女）", required = false) @RequestParam(required = false) Integer sex,
                @ApiParam(value = "当前积分", required = false) @RequestParam(required = false) Integer currentPoint,
                @ApiParam(value = "累计消费金额", required = false) @RequestParam(required = false) BigDecimal monetary,
                @ApiParam(value = "店铺id", required = false) @RequestParam(required = false) Integer storeId,
                @ApiParam(value = "家庭住址", required = false) @RequestParam(required = false) String familyAddress,
                @ApiParam(value = "Email", required = false) @RequestParam(required = false) String email,
                @ApiParam(value = "QQ", required = false) @RequestParam(required = false) String qq,
                @ApiParam(value = "会员等级", required = false) @RequestParam(required = false) Integer level,
                @ApiParam(value = "折扣比例", required = false) @RequestParam(required = false) Integer discount,
                @ApiParam(value = "创建人", required = false) @RequestParam(required = false) Integer author,
                @ApiParam(value = "创建时间", required = false) @RequestParam(required = false) Date createTime,
                @ApiParam(value = "修改人", required = false) @RequestParam(required = false) Integer modifier,
                @ApiParam(value = "修改时间", required = false) @RequestParam(required = false) Date modifyTime,
                @ApiParam(value = "状态，1有效，0无效，-1删除", required = false) @RequestParam(required = false) Integer status
        ){
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("cardNo", cardNo);
        params.put("mobile", mobile);
        params.put("memberName", memberName);
        params.put("sex", sex);
        params.put("currentPoint", currentPoint);
        params.put("monetary", monetary);
        params.put("storeId", storeId);
        params.put("familyAddress", familyAddress);
        params.put("email", email);
        params.put("qq", qq);
        params.put("level", level);
        params.put("discount", discount);
        params.put("author", author);
        params.put("createTime", createTime);
        params.put("modifier", modifier);
        params.put("modifyTime", modifyTime);
        params.put("status", status);
        List<MemberEntity> list = memberService.listByCondition(params);
        return R.ok().put("list", list);
    }

    /**
     * 查询详情
     */
    @ApiOperation(value = "查询详情", response = MemberEntity.class)
    @GetMapping("/info/{id}")
    @RequiresPermissions("bus:member:info")
    public R info(@PathVariable("id") Integer id){
        MemberEntity member = memberService.queryOneById(id);
        return R.ok().put("member", member);
    }

    /**
     * 新增保存
     */
    @ApiOperation(value = "新增保存", response = R.class)
    @PostMapping("/save")
    @RequiresPermissions("bus:member:save")
    public R save(@RequestBody MemberEntity entity){
        memberService.insert(entity);
        return R.ok();
    }

    /**
     * 修改保存
     */
    @ApiOperation(value = "修改保存", response = R.class)
    @PostMapping("/update")
    @RequiresPermissions("bus:member:update")
    public R update(@RequestBody MemberEntity entity){
		memberService.updateById(entity);
        return R.ok();
    }

    /**
     * 批量逻辑删除
     */
    @ApiOperation(value = "批量逻辑删除", response = R.class)
    @PostMapping("/delete")
    @RequiresPermissions("bus:member:delete")
    public R delete(@RequestBody Integer[] ids){
		memberService.deleteLogic(Arrays.asList(ids));
        return R.ok();
    }

}
